package com.evolutioninc.wordfinder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;

public class StringUtil {
	private static int MAX_WORD_SIZE = 8;
	private static int MIN_WORD_SIZE = 4;
	
	protected static ArrayList<HashMap<String, List<String>>> dictionary =
			new ArrayList<HashMap<String, List<String>>>();
	
	public static boolean compareStr(String word, String str) {
		if(str == null || str.length() == 0)
			return false;
		
		boolean found = false;
		for (int i = 0; i < str.length(); i++) {
			found = false;
			for (int j = 0; j < word.length(); j++) {
				if(word.charAt(j) == str.charAt(i)){
					found = true;
					break;
				}
			}
			if(!found)
				return false;
		}
		return true;
	}
	
	/**
	 * Sort a word by letters
	 * @param word - word to sort
	 * @return - word that where the letters are sorted
	 */
	public static String sortWords(String word){
		String sortWord = word.toLowerCase();
		char[] content = sortWord.toCharArray();
		java.util.Arrays.sort(content);
		sortWord = new String(content);
		return sortWord;
	}
	
	public static ArrayList<HashMap<String, List<String>>> createDictionary(ArrayList<String> al){
		ArrayList<HashMap<String, List<String>>> myMultiMap = new ArrayList<HashMap<String, List<String>>>(StringUtil.getMaxWordSize()+1);
		
		for(int i = 0; i < StringUtil.getMaxWordSize()+1; i++){
			myMultiMap.add(new HashMap<String, List<String>>());
		}
		
		ListIterator<String> li = al.listIterator();
		while(li.hasNext()){
			String word = li.next();
			if(word.length() > StringUtil.getMaxWordSize())
				continue;
				
			String key = sortWords(word);
			List<String> listWords;
			if(!myMultiMap.get(key.length()).containsKey(key)){
				listWords = new ArrayList<String>();
			}else{
				listWords = myMultiMap.get(key.length()).get(key);
			}
			
			listWords.add(word);
			myMultiMap.get(key.length()).put(key, listWords);
		}
		
		return myMultiMap;
	}

	public static void createDictionary(InputStreamReader inputReader){
		
		for(int i = 0; i <= StringUtil.getMaxWordSize(); i++){
			dictionary.add(new HashMap<String, List<String>>());
		}

		String line = "";
		String key = "";
		try {
			BufferedReader br = new BufferedReader(inputReader);
			while((line = br.readLine()) != null) {
				if(line.length() > StringUtil.getMaxWordSize())
					continue;
				
				key = sortWords(line);
				List<String> listWords;
				
				if(dictionary.get(key.length()).containsKey(key)){
					listWords = dictionary.get(key.length()).get(key);
				}
				else{
					listWords = new ArrayList<String>(1);
				}

				listWords.add(line);
				dictionary.get(key.length()).put(key, listWords);
			}
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}

	public static void setMaxWordSize(int mAX_WORD_SIZE) {
		MAX_WORD_SIZE = mAX_WORD_SIZE;
	}
	
	public static int getMaxWordSize() {
		return MAX_WORD_SIZE;
	}

	public static void setMinWordSize(int mIN_WORD_SIZE) {
		MIN_WORD_SIZE = mIN_WORD_SIZE;
	}

	public static int getMinWordSize() {
		return MIN_WORD_SIZE;
	}
}
